import traceback
from sqlalchemy.orm import Session
from sqlalchemy.sql import func

from app.common import exceptions as excs
from app.common.enums.resp_enum import ErrTypes
from app.common.enums.bz_enum import SubAccType
from app.models import PayProduct

import logging
logger = logging.getLogger(__name__)

def get_pay_product_by_channel_code(db: Session, channel_code: str):
    try:
        if not channel_code:
            raise excs.GenericException(error_type=ErrTypes.bz_param_validation_error)
        product = db.query(PayProduct).filter(PayProduct.pay_channel_code == channel_code).first()
        return product
    except excs.GenericException as err:
        logger.error('Error: %s' % err.message)
        raise excs.GenericException(err.error_type)
    except Exception as err:
        logger.exception(err)
        #logger.error(traceback.print_exc)
        raise excs.GenericException(ErrTypes.db_query_error)

def get_pay_products(db: Session, pay_type: int):
    try:
        if not pay_type:
            raise excs.GenericException(error_type=ErrTypes.bz_param_validation_error)
        products = db.query(PayProduct).filter(PayProduct.pay_type == pay_type).all()
        return products
    except excs.GenericException as err:
        logger.error('Error: %s' % err.message)
        raise excs.GenericException(err.error_type)
    except Exception as err:
        logger.exception(err)
        #logger.error(traceback.print_exc)
        raise excs.GenericException(ErrTypes.db_query_error)
    
def get_pay_product(db: Session, product_code: str):
    try:
        if not product_code:
            raise excs.GenericException(error_type=ErrTypes.bz_param_validation_error)
        product = db.query(PayProduct).filter(PayProduct.product_code == product_code).first()
        return product
    except excs.GenericException as err:
        logger.error('Error: %s' % err.message)
        raise excs.GenericException(err.error_type)
    except Exception as err:
        logger.exception(err)
        #logger.error(traceback.print_exc)
        raise excs.GenericException(ErrTypes.db_query_error)
    



